<template>
	<view>
		<view class="bgcolor">
			<cu-custom bgColor="bgmain" :isBack="true">
				<view slot="content">消息列表</view>
			</cu-custom>
		</view>
		<!-- <z-nav-bar transparentFixedFontColor="#000"  backState="2000" title="消息列表"></z-nav-bar> -->
		<view class="cu-list menu-avatar">
			<view class="cu-item" :class="modalName=='123move-box-'+ index?'move-cur':''" v-for="(item,index) in chatList"
				:key="index" @touchstart="ListTouchStart" @touchmove="ListTouchMove" @touchend="ListTouchEnd"
				:data-target="'move-box-' + index" @click="goChat(item.chat_user.u_id)">
				<view class="cu-avatar round lg"
					:style="[{backgroundImage:'url(' + item.chat_user.avatar + ')'}]">
				</view>
				<view class="content">
					<view class="text-grey">{{item.chat_user.nickname}}</view>
					<view class="text-gray text-sm text-cut" style="height: 60rpx;">
						<!-- <text class="cuIcon-infofill text-red  margin-right-xs"></text> 消息未送达 -->
						
						<rich-text :nodes="item.last_info"></rich-text>
					</view>
				</view> 
				<view class="action">
					<view class="text-grey text-xs">{{ item.record_time }}</view>
					<view class="cu-tag round bg-grey sm" v-if="item.unread_num > 0">{{ item.unread_num }}</view>
				</view>
				<!-- <view class="move">
					<view class="bg-red" @click.stop="removeChat(item.id)">删除</view>
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	var app = getApp();
	export default {
		data() {
			return {
				modalName: null,
				listTouchStart: 0,
				listTouchDirection: null,
				skin: false,
				userInfo: {},// 用户数据
				myuid: '', // 用户id
				chatList: [], // 存储会话列表
			};
		},
		onLoad() {
			
		},
		onShow() {
			// uni.showToast({
			// 	title:uni.getStorageSync('dmhu_id')
			// })
			uni.$emit('updateData', {})
			// 检测登录
			
			// this.judgeLogin(()=>{
				// this.userInfo = uni.getStorageSync('userinfo');
				// this.myuid = uni.getStorageSync('userinfo').uid
				var that = this;
				that.myuid = uni.getStorageSync('dmhu_id'),   
				// 	that.list = []
				// 	app.globalData.util.request({
				// 		url: 'Dmhlock/index',
				// 		method: 'POST',
				// 		data: {
				// 			s_id: uni.getStorageSync('schoolId'),
				// 			page: that.page,
				// 			// types: that.types,
				// 			// sortstate:that.sortstate,
				// 			latitude: uni.getStorageSync('latitude'),
				// 			longitude: uni.getStorageSync('longitude'),
				// 		},
				
				// 		success(res) {
				// 			console.log(res.data.data);
				// 			that.list = res.data.data.list
				// that.list = [...that.list,...res.data.data.list]
				// 		}
				// 	});
				this.getChatList()
				uni.hideTabBarRedDot({index:2})
			// });
		},
		methods: {
			// ListTouch触摸开始
			ListTouchStart(e) {
				this.listTouchStart = e.touches[0].pageX
			},

			// ListTouch计算方向
			ListTouchMove(e) {
				this.listTouchDirection = e.touches[0].pageX - this.listTouchStart > 0 ? 'right' : 'left'
			},

			// ListTouch计算滚动
			ListTouchEnd(e) {
				if (this.listTouchDirection == 'left') {
					this.modalName = e.currentTarget.dataset.target
				} else {
					this.modalName = null
				}
				this.listTouchDirection = null
			},
			// 获取会话列表
			getChatList(){
				var that = this
				app.globalData.util.request({
				    url: 'ZhChat/list',
				    data: {
				        // a_id: this.options.id,
				    },
				    success(res) {
				//         console.log(res);
				//         var index = 0;
				
				//         for (var i = 0; i < self.school.length; i++) {
				//             if (self.school[i].s_id == res.data.data.s_id) {
				//                 self.setData({
				//                     index: String(i)
				//                 });
				//             }
				//         }
				
				//         self.setData({
				//             form: res.data.data
				//         });
						that.chatList = res.data.data
				    }
				},true);
				// that.$http.get('api/ZhChat/list').then(res=> {
				// 	that.chatList = res
				// })
			},
			// 移除会话
			removeChat(id){
				var that = this
					app.globalData.util.request({
				url: 'ZhChat/removeChat',
				data: {
					'chat_id': id

					// a_id: this.options.id,
				},
				success(res) {
					//         console.log(res);
					//         var index = 0;

					//         for (var i = 0; i < self.school.length; i++) {
					//             if (self.school[i].s_id == res.data.data.s_id) {
					//                 self.setData({
					//                     index: String(i)
					//                 });
					//             }
					//         }

					//         self.setData({
					//             form: res.data.data
					//         });
					that.chatList = res.data.data
				}
			}, true);
				// that.$http.post('api/ZhChat/removeChat',{
				// 	'chat_id': id
				// }).then(res=> {
				// 	that.chatList = []
				// 	that.getChatList()
				// })
			}, 
			// 前往聊天
			goChat(sid){
				console.log('前往聊天',sid)
				uni.navigateTo({
					url: '/gc_school/pages/chat/chat?s_id=' + sid
				})
			}
		}
	}
</script>

<style>
	.page {
		height: 100Vh;
		width: 100vw;
	}

	.page.show {
		overflow: hidden;
	}

	.switch-sex::after {
		content: "\e716";
	}

	.switch-sex::before {
		content: "\e7a9";
	}

	.switch-music::after {
		content: "\e66a";
	}

	.switch-music::before {
		content: "\e6db";
	}
</style>
